namespace SmartSoft.ADP
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class CTDHDataAccess
    {
        private WSCTDHDataAccess.WSCTDHDataAccess wDataAcess = new WSCTDHDataAccess.WSCTDHDataAccess();
        private SmartSoft.DAL.CTDHDataAccess oDataAcess = new SmartSoft.DAL.CTDHDataAccess();

        public CTDHDataAccess()
        {
            wDataAcess.Url = wDataAcess.Url.Replace("localhost", AdapterInstance.ServerIp);
        }

        public int CheckInvoiceLock(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceLock(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public int CheckInvoicePost(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoicePost(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoicePost(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public int CheckInvoiceStatus(string ps_Subsidiary, string ps_UserName, object ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.CheckInvoiceStatus(ps_Subsidiary, ps_UserName, ps_InvoiceOrder);
            }
        }

        public object Cong_So_Ct(string ps_Subsidiary, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Cong_So_Ct(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct == DBNull.Value ? string.Empty : ps_So_Ct);
            }
            else
            {
                return oDataAcess.Cong_So_Ct(ps_Subsidiary, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct);
            }
        }

        public bool Delete(string ps_UserName, object po_KeyCode)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Delete(ps_UserName, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
            }
            else
            {
                return oDataAcess.Delete(ps_UserName, po_KeyCode);
            }
        }

        //public bool DeleteDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.DeleteDetailDataRow(po_Connection, po_Transaction, po_KeyCode == DBNull.Value ? string.Empty : po_KeyCode);
        //    }
        //    else
        //    {
        //        return oDataAcess.DeleteDetailDataRow(po_Connection, po_Transaction, po_KeyCode);
        //    }
        //}

        public DataSet GetDetailDataSet(string ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetDetailDataSet(ps_InvoiceOrder);
            }
            else
            {
                return oDataAcess.GetDetailDataSet(ps_InvoiceOrder);
            }
        }

        public DataTable GetDetailPOrder(object ps_MasterKey, object ps_ParentKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailPOrder(ps_MasterKey, ps_ParentKey);
            }
            else
            {
                table= oDataAcess.GetDetailPOrder(ps_MasterKey, ps_ParentKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailQuotation(object ps_MasterKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailQuotation(ps_MasterKey);
            }
            else
            {
                table= oDataAcess.GetDetailQuotation(ps_MasterKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailRequire(object ps_InvoiceOrder)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailRequire(ps_InvoiceOrder);
            }
            else
            {
                table= oDataAcess.GetDetailRequire(ps_InvoiceOrder);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailRequire(object ps_MasterKey, object ps_ParentKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailRequire(ps_MasterKey, ps_ParentKey);
            }
            else
            {
                table= oDataAcess.GetDetailRequire(ps_MasterKey, ps_ParentKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetDetailSOrder(object ps_MasterKey, object ps_ParentKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetDetailSOrder(ps_MasterKey, ps_ParentKey);
            }
            else
            {
                table= oDataAcess.GetDetailSOrder(ps_MasterKey, ps_ParentKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetItemBalance(object ps_MasterKey)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetItemBalance(ps_MasterKey);
            }
            else
            {
                table= oDataAcess.GetItemBalance(ps_MasterKey);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataRow GetMasterDataRow(string ps_InvoiceOrder)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return SmartSoft.DAL.SSDataAccess.ConvertTableToRow(wDataAcess.GetMasterDataRow(ps_InvoiceOrder));
            }
            else
            {
                return oDataAcess.GetMasterDataRow(ps_InvoiceOrder);
            }
        }

        public DataTable GetMasterPOrder(bool pb_FirstTime, string ps_Subsidiary, object ps_ParentKey, object pd_Ngay_Ct, object ps_Ma_Dt)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterPOrder(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct, ps_Ma_Dt);
            }
            else
            {
                table= oDataAcess.GetMasterPOrder(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct, ps_Ma_Dt);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetMasterQuotation(bool pb_FirstTime, string ps_Subsidiary, object ps_ParentKey, object ps_InvoiceType, object pd_Ngay_Ct, object ps_Ma_Dt)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterQuotation(pb_FirstTime, ps_Subsidiary, ps_ParentKey, ps_InvoiceType, pd_Ngay_Ct, ps_Ma_Dt);
            }
            else
            {
                table= oDataAcess.GetMasterQuotation(pb_FirstTime, ps_Subsidiary, ps_ParentKey, ps_InvoiceType, pd_Ngay_Ct, ps_Ma_Dt);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetMasterRequire(bool pb_FirstTime, string ps_Subsidiary, object ps_ParentKey, object pd_Ngay_Ct)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterRequire(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct);
            }
            else
            {
                table= oDataAcess.GetMasterRequire(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetMasterSOrder(bool pb_FirstTime, string ps_Subsidiary, object ps_ParentKey, object pd_Ngay_Ct, object ps_Ma_Dt)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterSOrder(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct, ps_Ma_Dt);
            }
            else
            {
                table= oDataAcess.GetMasterSOrder(pb_FirstTime, ps_Subsidiary, ps_ParentKey, pd_Ngay_Ct, ps_Ma_Dt);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public DataTable GetMasterTable(string ps_Subsidiary, int pi_DaysFilter, object pd_Ngay_Ct1, object pd_Ngay_Ct2, object ps_So_Ct1, object ps_So_Ct2, object ps_Ma_Ct, object ps_Ma_Tte, object ps_Ma_Nv, object ps_Ma_Bp, object ps_Ma_Hd, object ps_Ma_Dt, object ps_Ma_Vt, object ps_Dien_Giai, object ps_Ma_Loai1, object ps_Ma_Loai2, object ps_Ma_Loai3, int ii_FilterByUser, string ps_UserName, object ps_Notes, int pi_Posted, int pi_Locked)
        {
            DataTable table = null;
            if (AdapterInstance.IsRemoteConnected)
            {
                table= wDataAcess.GetMasterTable(ps_Subsidiary, pi_DaysFilter, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct1, ps_So_Ct2, ps_Ma_Ct, ps_Ma_Tte, ps_Ma_Nv, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_Ma_Vt, ps_Dien_Giai, ps_Ma_Loai1, ps_Ma_Loai2, ps_Ma_Loai3, ii_FilterByUser, ps_UserName, ps_Notes, pi_Posted, pi_Locked);
            }
            else
            {
                table= oDataAcess.GetMasterTable(ps_Subsidiary, pi_DaysFilter, pd_Ngay_Ct1, pd_Ngay_Ct2, ps_So_Ct1, ps_So_Ct2, ps_Ma_Ct, ps_Ma_Tte, ps_Ma_Nv, ps_Ma_Bp, ps_Ma_Hd, ps_Ma_Dt, ps_Ma_Vt, ps_Dien_Giai, ps_Ma_Loai1, ps_Ma_Loai2, ps_Ma_Loai3, ii_FilterByUser, ps_UserName, ps_Notes, pi_Posted, pi_Locked);
            }
            if (table != null) table.TableName = string.Empty;
            return table;
        }

        public object GetTruePrice(string ps_Subsidiary, object ps_InvoiceType, object pd_Ngay_Ct, object ps_Ma_Dt, object ps_Ma_Vt, ref object ps_DetailKey)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.GetTruePrice(ps_Subsidiary, ps_InvoiceType, pd_Ngay_Ct, ps_Ma_Dt, ps_Ma_Vt, ref ps_DetailKey);
            }
            else
            {
                return oDataAcess.GetTruePrice(ps_Subsidiary, ps_InvoiceType, pd_Ngay_Ct, ps_Ma_Dt, ps_Ma_Vt, ref ps_DetailKey);
            }
        }

        public void Lock(string ps_UserName, object ps_InvoiceOrder, bool pb_Locked)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                wDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
            else
            {
                oDataAcess.Lock(ps_UserName, ps_InvoiceOrder, pb_Locked);
            }
        }

        public bool NumberExisted(string ps_Subsidiary, bool pb_CheckNewRow, string ps_InvoiceOrder, string ps_InvoiceCode, object pd_Ngay_Ct, object ps_So_Ct)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct == DBNull.Value ? string.Empty : ps_So_Ct);
            }
            else
            {
                return oDataAcess.NumberExisted(ps_Subsidiary, pb_CheckNewRow, ps_InvoiceOrder, ps_InvoiceCode, pd_Ngay_Ct, ps_So_Ct);
            }
        }

        public bool Post(string ps_UserName, object ps_InvoiceOrder, object pi_Posted)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Post(ps_UserName, ps_InvoiceOrder, pi_Posted);
            }
            else
            {
                return oDataAcess.Post(ps_UserName, ps_InvoiceOrder, pi_Posted);
            }
        }

        public bool Save(bool pb_InvoiceHistory, bool pb_NewChecked, string ps_Subsidiary, string ps_UserName, DataRow po_MasterDataRow, DataTable po_DetailTable, ref bool pb_CheckOverFlow)
        {
            if (AdapterInstance.IsRemoteConnected)
            {
                return wDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, SmartSoft.DAL.SSDataAccess.ConvertRowToTable(po_MasterDataRow), po_DetailTable, ref pb_CheckOverFlow);
            }
            else
            {
                return oDataAcess.Save(pb_InvoiceHistory, pb_NewChecked, ps_Subsidiary, ps_UserName, po_MasterDataRow, po_DetailTable, ref pb_CheckOverFlow);
            }
        }

        //public int SaveDetailDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveDetailDataRow(po_Connection, po_Transaction, po_MasterKey, row);
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveDetailDataRow(po_Connection, po_Transaction, po_MasterKey, row);
        //    }
        //}

        //public int SaveMasterDataRow(SqlConnection po_Connection, SqlTransaction po_Transaction, bool pb_NewChecked, string ps_Subsidiary, DataRow row)
        //{
        //    if (AdapterInstance.IsRemoteConnected)
        //    {
        //        return wDataAcess.SaveMasterDataRow(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //    else
        //    {
        //        return oDataAcess.SaveMasterDataRow(po_Connection, po_Transaction, pb_NewChecked, ps_Subsidiary, row);
        //    }
        //}
    }
}